package cn.jimmie.learn.viewArt.refreshview.core.recyclerview;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import cn.jimmie.learn.viewArt.refreshview.core.ContentView;
import cn.jimmie.learn.viewArt.refreshview.core.RefreshState;


/**
 * function :
 *
 * <p> Create Time:  2019年04月13日 14:33 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class RecyclerContentView implements ContentView {
    private RecyclerView mView;
    private OnFootLocationListener mListener;
    private RefreshState mStatus;
    private boolean isFooterAdded;

    private View mFootView;

    public RecyclerContentView(View view, RefreshState status) {
        this.mView = (RecyclerView) view;
        this.mStatus = status;
        this.mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                handleScrollStateChanged(newState);
            }
        });
    }

    @Override
    public boolean isTopPosition() {
        // RecyclerView 不可见, 则可以触发刷新
        if (mView.getVisibility() != View.VISIBLE) {
            return true;
        }

        return !mView.canScrollVertically(-1);
    }

    @Override
    public void setFooterVisibility(View view, int visible) {
        if (!mStatus.isFootViewEnable()) return;

        this.mFootView = view;
        addFooterView(view);
        view.setVisibility(visible);
    }

    @Override
    public void setOnFooterLocationListener(OnFootLocationListener listener) {
        this.mListener = listener;
    }

    @Override
    public void release() {

    }

    @Override
    public void removeFootView(View v) {
        if (isFooterAdded) {
            mView.removeView(v);
            RecyclerView.Adapter adapter = mView.getAdapter();
            if (!(adapter instanceof RecyclerFooterAdapter)) return;
            RecyclerFooterAdapter wrapper = (RecyclerFooterAdapter) adapter;
            wrapper.setFootView(null);
        }
    }

    private void handleScrollStateChanged(int newState) {
        if (!mStatus.isFootViewEnable()) return;

        addFooterView(mFootView);

        // 1. 滑动未停止, 不触发
        // 2. 刷新未停止, 不触发
        // 3. 刷新结果为 END, 不触发
        // 4. 没有滑动到底部, 不触发
        if (newState != RecyclerView.SCROLL_STATE_IDLE ||
                mStatus.getLoadState() != RefreshState.LoadState.IDLE ||
                mStatus.getResultState() == RefreshState.ResultState.END
                || mView.canScrollVertically(1)
        ) return;

        // 没有占满一屏, 则不显示加载更多
        if (!overFullLayout()) return;

        mStatus.setLoadState(RefreshState.LoadState.PULLING);
        if (mListener != null) mListener.onFooterRefresh();
    }

    private void addFooterView(final View view) {
        if (!isFooterAdded) {
            if (mView.getAdapter() == null) return;

            RecyclerView.Adapter adapter = mView.getAdapter();
            RecyclerFooterAdapter wrapper = new RecyclerFooterAdapter(adapter);
            mView.setAdapter(wrapper);
            wrapper.setFootView(view);

            // 为了解决 mView footView 不能占满一项的宽度,导致布局有问题.
//            view.post(new Runnable() {
//                @Override
//                public void run() {
//                    view.getLayoutParams().width = mView.getWidth();
//                    view.requestLayout();
//                }
//            });

            isFooterAdded = true;
        }
    }

    /**
     * 是否占满了一屏
     */
    private boolean overFullLayout() {
        int layoutHeight = mView.getHeight();

        int childHeight = mView.getPaddingBottom() + mView.getPaddingTop();
        int count = mView.getChildCount();
        for (int i = 0; i < count; i++) {
            int margin = 0;
            View child = mView.getChildAt(i);
            ViewGroup.LayoutParams params = child.getLayoutParams();
            if (params instanceof ViewGroup.MarginLayoutParams) {
                ViewGroup.MarginLayoutParams tmp = (ViewGroup.MarginLayoutParams) params;
                margin = tmp.bottomMargin + tmp.topMargin;
            }
            childHeight += child.getHeight() + margin;
            if (childHeight > layoutHeight) return true;
        }
        return childHeight > layoutHeight;
    }
}
